package cn.wolfcode.shop.server.service.impl;

import cn.wolfcode.shop.api.domain.OrderInfo;
import cn.wolfcode.shop.api.domain.OrderProduct;
import cn.wolfcode.shop.api.query.PageResult;
import cn.wolfcode.shop.api.query.QueryObject;
import cn.wolfcode.shop.api.service.IOrderActionService;
import cn.wolfcode.shop.api.service.IOrderinfoService;
import cn.wolfcode.shop.server.mapper.OrderInfoMapper;
import cn.wolfcode.shop.server.mapper.OrderProductMapper;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrderinfoServiceImpl implements IOrderinfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Reference
    private IOrderActionService orderActionService;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public int update(OrderInfo orderInfo) {
        return orderInfoMapper.updateByPrimaryKey(orderInfo);
    }

    @Override
    public List<OrderInfo> getAll() {
        return orderInfoMapper.selectAll();
    }

    @Override
    public OrderInfo get(Long id) {
        return orderInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = orderInfoMapper.queryForCount(qo);
        if (count > 0){
            List<OrderInfo> list = orderInfoMapper.queryForList(qo);
            return new PageResult(list, count, qo.getCurrentPage(), qo.getPageSize());
        }
        return PageResult.EMPTY_PAGE;
    }

    @Override
    public void changeOrderStatus(Long id, Byte state, String remark, String deliverSn) {
        OrderInfo orderInfo = this.get(id);
        if (orderInfo != null){
            if (state == 1 && orderInfo.getOrderStatus() == OrderInfo.ORDER_NORMAL){
                orderInfo.setOrderStatus((byte) OrderInfo.ORDER_CONFIRM);
                orderInfo.setPayStatus((byte) OrderInfo.PAY_CONFIRM);
                orderActionService.createAction(orderInfo, remark);
            }else if (state == 2){
                orderInfo.setOrderStatus((byte) OrderInfo.ORDER_CANCEL);
                orderActionService.createAction(orderInfo, remark);
            }else if (state == 3){
                orderInfo.setOrderStatus((byte) OrderInfo.ORDER_INVALID);
                orderActionService.createAction(orderInfo, remark);
            }else if (state == 4 && orderInfo.getShippingStatus() == OrderInfo.SHOP_NORMAL){
                orderInfo.setShippingStatus((byte) OrderInfo.SHOP_CONFIRM);
                orderInfo.setDeliverSn(deliverSn);
                orderInfo.setSendTime(new Date());
                orderActionService.createAction(orderInfo, remark);
            }else if (state == 5 && orderInfo.getShippingStatus() == OrderInfo.SHOP_CONFIRM){
                orderInfo.setShippingStatus((byte) OrderInfo.SHOP_FINISH);
                orderActionService.createAction(orderInfo, remark);
            } else if (state == 6 && orderInfo.getShippingStatus() == OrderInfo.SHOP_FINISH) {
                orderActionService.createAction(orderInfo, remark);
            }
            this.update(orderInfo);
        }
    }

    @Override
    public void productUpdate(OrderInfo orderInfo) {
        OrderInfo info = this.get(orderInfo.getId());
        List<OrderProduct> oldProducts = info.getOrderProducts();
        List<OrderProduct> products = orderInfo.getOrderProducts();
        BigDecimal amount = BigDecimal.ZERO;
        for (int i = 0; i < products.size(); i++){
            oldProducts.get(i).setProductPrice(products.get(i).getProductPrice());
            oldProducts.get(i).setProductNumber(products.get(i).getProductNumber());
            amount = amount.add(products.get(i).getProductPrice().multiply(BigDecimal.valueOf(products.get(i).getProductNumber())));
            orderProductMapper.updateByPrimaryKey(oldProducts.get(i));
        }
        info.setGoodsAmount(amount);
        info.setOrderAmount(amount.add(OrderInfo.FREIGHT));
        this.update(info);
    }

    @Override
    public void orderInfoUpdate(OrderInfo orderInfo) {
        OrderInfo info = this.get(orderInfo.getId());
        info.setConsignee(orderInfo.getConsignee());
        info.setProvince(orderInfo.getProvince());
        info.setCity(orderInfo.getCity());
        info.setDistrict(orderInfo.getDistrict());
        info.setAddress(orderInfo.getAddress());
        info.setZipcode(orderInfo.getZipcode());
        info.setMobile(orderInfo.getMobile());
        this.update(info);
    }
}
